package com.controller;

import java.io.File;


import java.io.IOException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.entity.KeHu;
import com.entity.Sysuser;
import com.entity.Record;
import com.server.RecordServer;
import com.server.KeHuServer;
import com.server.SysuserServier;
import com.util.PageBean;

@Controller
public class RecordController {
	@Resource
	private RecordServer recordService;
	@Resource
	private SysuserServier userService;
	@Resource
	private KeHuServer keHuService;
	

//	文件上传
	public String fileUpload(@RequestParam(value="file",required=false)MultipartFile file,
			HttpServletRequest request,String img){
		String path=request.getSession().getServletContext().getRealPath("upload");
		System.out.println("path==="+path);
		System.out.println("file==="+file);
		String fileName=file.getOriginalFilename();
		System.out.println("fileName==="+fileName);
		File targetFile=new File(path,fileName);
		if(!targetFile.exists()){
			targetFile.mkdirs();
		}
		try {
			file.transferTo(targetFile);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String pa=request.getContextPath()+"/upload/"+fileName;
		System.out.println("path==="+pa);
		if(fileName!=null&&!fileName.equals("")){
			img=fileName;
		}else{
			img=null;	
		}
		
		return img;
		
	}
	@RequestMapping("admin/doAddRecord.do")
	public String doAddRecord(ModelMap map){
		map.put("klist", keHuService.getAll(null));
		return "admin/add_record";
	}
//	
	@RequestMapping("admin/addRecord.do")
	public String addRecord(HttpServletRequest request,Record yp,HttpSession session){
		Sysuser u=(Sysuser)session.getAttribute("auser");
		Timestamp time=new Timestamp(System.currentTimeMillis());
		if(u==null){
			return "admin/nologin";
		}else{
			yp.setUid(u.getUid());
			yp.setStatus("已拜访");
			yp.setIsdel("1");
			yp.setPubtime(time.toString().substring(0, 19));
			recordService.add(yp);
			return "admin/success";
		}

	}
	@RequestMapping("admin/doUpdateRecord.do")
	public String doUpdateRecord(int id,ModelMap map){
		map.put("rec", recordService.getById(id));
		map.put("ulist", userService.getAll(null));
		map.put("klist", keHuService.getAll(null));
		return "admin/update_record";
	}
	@RequestMapping("admin/updateRecord.do")
	public String updateRecord(HttpServletRequest request,Record yp){
		recordService.update(yp);
		return "redirect:RecordList.do";
	}
//   分页查询
	@RequestMapping("admin/RecordList.do")
	public String RecordList(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Record yp){
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page),PageBean.PAGESIZE);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", pageBean.getPageSize());
		int total=recordService.getCount(null);
		pageBean.setTotal(total);
		List<Record> list=recordService.getByPage(pmap);
		List<Sysuser> ulist=userService.getAll(null);
		List<KeHu> jzlist=keHuService.getAll(null);
		map.put("page", pageBean);
		map.put("list", list);
		map.put("ulist", ulist);
		map.put("klist", jzlist);
		session.setAttribute("p", 1);
		return "admin/list_record";
	}
	
	@RequestMapping("admin/deleteRecord.do")
	public String deleteRecord(int id){
		recordService.delete(id);
		return "redirect:RecordList.do";
	}
}
